/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.apache.dict.service.plugins  
 */
package com.apache.dict.service.plugins;

import java.util.List;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.dict.entity.DataItem;
import com.apache.dict.manager.CacheTacticsPlugin;
import com.apache.dict.manager.DataItemManager;
import com.apache.dict.vo.ItemListVo;
import com.apache.tools.ClassToolsUtil;

/**
 * description:  
 * @author dyhou 创建时间：2016-9-27  
 */
public class DictItemActionPluginImpl implements PluginConnector {

	private DataItemManager dataItemManager;

	private CacheTacticsPlugin cacheTactics;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		String exeType = String.valueOf(param.getParams("exeType"));
		DataItem item = new DataItem();
		item = (DataItem) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), item);
		ResultEntity result = new ResultEntity();

		boolean mark = false;
		if ("insert".equalsIgnoreCase(exeType) || "update".equalsIgnoreCase(exeType)) {
			mark = saveItemInfo(item, result);
		} else if ("del".equalsIgnoreCase(exeType)) {
			mark = delDataItem(item, result);
		} else if ("view".equalsIgnoreCase(exeType)) {
			viewItem(item, result);
		} else if ("initCache".equalsIgnoreCase(exeType)) {//初始化数据缓存
			cacheTactics.initItem();
			mark = true;
		} else {
			String fatherId = String.valueOf(param.getParams("id"));
			if (Validator.isNull(item.getFatherId())) {
				item.setFatherId(fatherId);
			}
			treeItems(item, result);
		}
		if (mark) {
			result.setEntity(mark);
		}
		result.setResult("true");
		return result;
	}

	private void treeItems(DataItem item, ResultEntity result) {
		ItemListVo itemvo = new ItemListVo();
		ParamsVo vo = new ParamsVo();
		itemvo.setCateEname(item.getCateEname());
		if (Validator.isNotNull(item.getFatherId())) {
			itemvo.setFatherId(item.getFatherId());
		} else if (Validator.isNotNull(item.getFatherValue())) {
			itemvo.setFatherValue(item.getFatherValue());
		} else {
			itemvo.setFatherId("0");
		}

		if (Validator.isNotNull(item.getItemText())) {
			itemvo.setItemText(item.getItemText());
		}
		if (Validator.isNotNull(item.getItemValue())) {
			itemvo.setItemValue(item.getItemValue());
		}

		vo.setObj(itemvo);
		List<DataItem> list = dataItemManager.getList(vo);
		net.sf.json.JSONArray jary = new net.sf.json.JSONArray();
		for (int i = 0; i < list.size(); i++) {
			DataItem ditem = list.get(i);
			net.sf.json.JSONObject jvo = net.sf.json.JSONObject.fromObject(ditem);
			if (ditem.getSubCount() != null && ditem.getSubCount() > 0) {
				jvo.put("state", "closed");
			} else {
				jvo.put("state", "opened");
			}
			jary.add(jvo);
		}
		result.setMessage("查询成功！");
		result.setEntity(jary);
	}

	private boolean saveItemInfo(DataItem item, ResultEntity result) {
		ParamsVo vo = new ParamsVo();
		String itemId = item.getItemId();
		result.setEntity("false");
		if (Validator.isNull(itemId)) {//新增
			ParamsVo<DataItem> pvo = new ParamsVo<DataItem>();
			pvo.setMethodKey("checkCateEnameAndItemValue");
			pvo.setParams("cateEname", item.getCateEname());
			pvo.setParams("itemValue", item.getItemValue());
			Object obj = dataItemManager.execute(pvo);
			if ("true".equals(obj.toString())) {
				result.setMessage("新增条目值已经存在！");
				return false;
			}
			vo.setObj(item);
			itemId = dataItemManager.saveInfo(vo);
			if (Validator.isNull(itemId)) {
				result.setMessage("新增条目失败，请重试！");
				return false;
			} else {
				return true;
			}
		} else {//修改
			item.setSubCount(Integer.parseInt(Validator.getDefaultStr(item.getSubCount() + "", "0")));
			vo.setObj(item);
			boolean mark = dataItemManager.editInfo(vo);
			if (Validator.isNull(itemId)) {
				result.setMessage("修改条目失败，请重试！");
			}
			return mark;
		}
	}

	private boolean delDataItem(DataItem item, ResultEntity result) {
		result.setEntity("false");
		if (Validator.isNotNull(item.getItemId()) && Validator.isNotNull(item.getCateEname())) {
			ParamsVo<DataItem> itemvo = new ParamsVo<DataItem>();
			itemvo.setMethodKey("checkDataItemByFatherId");
			itemvo.setParams("cateEname", item.getCateEname());
			itemvo.setParams("fatherId", item.getItemId());
			Object obj = dataItemManager.execute(itemvo);
			if ("true".equals(obj.toString())) {

				result.setMessage("请先删除该科目下的子条目数据！");
				return false;
			}
		}
		if (Validator.isNotNull(item.getItemId())) {
			ParamsVo<DataItem> vo = new ParamsVo<DataItem>();
			vo.setInfoId(item.getItemId());
			boolean mark = dataItemManager.deleteInfo(vo);
			result.setEntity(mark);
			return mark;
		}
		result.setMessage("请先选择要删除的条目信息！");
		return false;
	}

	private void viewItem(DataItem item, ResultEntity result) {
		ParamsVo<DataItem> vo = new ParamsVo<DataItem>();
		if (Validator.isNotNull(item.getCateEname()) && Validator.isNotNull(item.getItemValue())) {
			vo.setMethodKey("ByCateEnameAndItemValue");
			Object obj = dataItemManager.execute(vo);
			result.setMessage("查询成功！");
			result.setResult("true");
			result.setEntity(obj);
		} else if (Validator.isNotNull(item.getItemId())) {
			vo.setInfoId(item.getItemId());
			Object obj = dataItemManager.getInfoById(vo);
			result.setMessage("查询成功！");
			result.setResult("true");
			result.setEntity(obj);
		} else {
			result.setMessage("查询失败！");
			result.setEntity("false");
		}
	}

	public void setDataItemManager(DataItemManager dataItemManager) {
		this.dataItemManager = dataItemManager;
	}

	public void setCacheTactics(CacheTacticsPlugin cacheTactics) {
		this.cacheTactics = cacheTactics;
	}
}
